//
//  FunctionView.m
//  GuoBang
//
//  Created by 梧桐树 on 16/4/28.
//  Copyright © 2016年 梧桐树. All rights reserved.
//

#import "FunctionView.h"

@implementation FunctionView
-(instancetype)initWithFrame:(CGRect)frame{
    
    
    self = [super initWithFrame:frame];
    if (self) {
         dataArry=@[@{@"icon":@"sy_ico_clock@2x.png",@"title":@"提醒"},@{@"icon":@"sy_ico_tel@2x.png",@"title":@"话费"},@{@"icon":@"sy_ico_coffer@2x.png",@"title":@"咖啡"},@{@"icon":@"sy_ico_car@2x.png",@"title":@"打车"},@{@"icon":@"sy_ico_tea@2x.png",@"title":@"下午茶"},@{@"icon":@"sy_ico_food@2x.png",@"title":@"零食"},@{@"icon":@"sy_ico_apple@2x.png",@"title":@"水果"},@{@"icon":@"sy_ico_more@2x.png",@"title":@"更多服务"}];
        //创建布局对象
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//        //设置单元格的尺寸
//        flowLayout.itemSize = CGSizeMake(80, 80);
//        //设置头视图高度
//        flowLayout.headerReferenceSize = CGSizeMake(0, 30);
        //flowlaout的属性，横向滑动
        //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        col = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 130) collectionViewLayout:flowLayout];
        col.delegate=self;
        col.dataSource=self;
        
        [self addSubview:col];
        [col registerClass:[FunctionCell class] forCellWithReuseIdentifier:@"FunctionCell"];
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, col.frame.size.height, WIDTH, 30)];
        lab.text=@"服务";
        lab.textColor=[SEATools stringToColor:@"ff6d6d"];
        lab.textAlignment=NSTextAlignmentCenter;
        lab.font=[UIFont systemFontOfSize:13];
        [self addSubview:lab];
        self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, WIDTH, col.frame.size.height+lab.frame.size.height);
        self.backgroundColor=[UIColor whiteColor];
        col.backgroundColor=[UIColor whiteColor];
    }
    return self;
}

#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataArry.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString * CellIdentifier = @"FunctionCell";
//    FunctionCell * cell = (FunctionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
//    
    UINib *nib = [UINib nibWithNibName:@"FunctionCell"
                                bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"FunctionCell"];
    FunctionCell *cell = [[FunctionCell alloc]init];
    
    // Set up the reuse identifier
    cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"FunctionCell"
                                                     forIndexPath:indexPath];
    cell.title.text=[[dataArry objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.icon.image=[UIImage imageNamed:[[dataArry objectAtIndex:indexPath.row] objectForKey:@"icon"]];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    return CGSizeMake(WIDTH/4, 60);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate selectFunction:indexPath.row];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
